package org.kl.bpm.listener;

import javax.annotation.Resource;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.impl.context.Context;
import org.kl.bpm.cmd.DelegateTaskCmd;
import org.kl.bpm.entity.conf.BpmDelegateInfo;
import org.kl.bpm.service.conf.BpmDelegateInfoService;

public class DelegateTaskListener extends DefaultTaskListener {
	private static final long serialVersionUID = 514104975243527158L;
	private BpmDelegateInfoService bpmDelegateInfoService;

	@Override
	public void onAssignment(DelegateTask delegateTask) throws Exception {
		String assignee = delegateTask.getAssignee();
		String processDefinitionId = delegateTask.getProcessDefinitionId();
		BpmDelegateInfo delegateInfo = bpmDelegateInfoService.findByAssigneeAndStatus(assignee, processDefinitionId);

		if (delegateInfo == null) {
			return;
		}

		String attorney = delegateInfo.getAttorney().getId();
		new DelegateTaskCmd(delegateTask.getId(), attorney).execute(Context.getCommandContext());
	}

	@Resource
	public void setBpmDelegateInfoService(BpmDelegateInfoService bpmDelegateInfoService) {
		this.bpmDelegateInfoService = bpmDelegateInfoService;
	}
}
